(function () {
    angular.module('app')
        .component('xxLogin', {
            templateUrl: './app/common/login/login.html',
            controller: ctrl
        });

    function ctrl($scope,$state,LoginFactory,urlCnst,userService) {

        var routes = {
            'CUSTOMER':'user.home',
            'STORE_MANAGER':'drug-manager.order-processing',
            'ROLE_SYSTEM_MANAGER':'user.home'
            // 'ROLE_SYSTEM_MANAGER':'system-manager.create-account'
        };

        $scope.login = function (body) {
            function cb(err, res) {
                console.log(err,res);
                //TODO set userService
                if (!err){
                    userService.setUser(res['res']);
                    var type = userService.user['userType'];
                    $state.go(routes[type]);
                }
            }
            if (!body['username'] || !body['password']) {
                urlCnst.notice('请输入用户名和密码');
                return;
            }
            LoginFactory.login(body, cb);
        };

        $scope.register = function () {
            $state.go('register')
        };
    }
})();
